package com.syg.logback.core;

import com.syg.boot.util.text.StringUtil;
import com.syg.boot.util.time.DateUtil;

import java.io.Serializable;
import java.util.Date;

/**
 * @author syg
 * @version 1.0
 * @date 2021年11月24日19:14:46
 */
public class BizTranBuilder implements Serializable {

    private static final long serialVersionUID = -7546789293857662784L;
    private String flowId;
    private String bizId;
    private String tranCd;
    private String idNo;
    private String name;
    private String channelNo;
    private String sendTime;
    private String receiveTime;
    private String requestData;
    private String responseData;
    private String exeSts = "S";
    private String respCode;
    public static final String EXE_STS_S = "S";
    public static final String EXE_STS_F = "F";

    public BizTranBuilder() {
    }

    public BizTranBuilder setFlowId(String flowId) {
        this.flowId = flowId;
        return this;
    }

    public BizTranBuilder setBizId(String bizId) {
        this.bizId = bizId;
        return this;
    }

    public BizTranBuilder setTranCd(String tranCd) {
        this.tranCd = tranCd;
        return this;
    }

    public BizTranBuilder setIdNo(String idNo) {
        this.idNo = idNo;
        return this;
    }

    public BizTranBuilder setName(String name) {
        this.name = name;
        return this;
    }

    public BizTranBuilder setChannelNo(String channelNo) {
        this.channelNo = channelNo;
        return this;
    }

    public BizTranBuilder setSendTime(String sendTime) {
        this.sendTime = sendTime;
        return this;
    }

    public BizTranBuilder setReceiveTime(String receiveTime) {
        this.receiveTime = receiveTime;
        return this;
    }

    public BizTranBuilder setRequestData(String requestData) {
        this.requestData = requestData;
        return this;
    }

    public BizTranBuilder setResponseData(String responseData) {
        this.responseData = responseData;
        return this;
    }

    public BizTranBuilder setExeSts(String exeSts) {
        this.exeSts = exeSts;
        return this;
    }

    public BizTranBuilder setExeStsSucc() {
        this.exeSts = "S";
        this.respCode = "0000000";
        return this;
    }

    public BizTranBuilder setExeStsFail() {
        this.exeSts = "F";
        return this;
    }

    public BizTranBuilder setExeStsFail(String excpetionMsg) {
        this.exeSts = "F";
        if (StringUtil.isAllBlank(excpetionMsg)) {
            this.respCode = "Z999999";
        }

        String[] array = excpetionMsg.split("##@##");
        if (array.length == 1) {
            String msg = array[0];
            int end = Math.min(msg.length(), 20);
            this.respCode = msg.substring(0, end);
        } else if (array.length >= 2) {
            this.respCode = array[0];
        }

        return this;
    }

    public BizTranBuilder setRespCode(String respCode) {
        this.respCode = respCode;
        return this;
    }

    public BizTranInf build() {
        if (StringUtil.isEmpty(this.sendTime)) {
            this.sendTime = DateUtil.DEFAULT_FORMAT.format(new Date());
        }

        if (StringUtil.isEmpty(this.receiveTime)) {
            this.receiveTime = DateUtil.DEFAULT_FORMAT.format(new Date());
        }

        if ("F".equals(this.exeSts) && StringUtil.isEmptyAll(this.respCode)) {
            this.respCode = "Z999999";
        } else if ("S".equals(this.exeSts) && StringUtil.isEmptyAll(this.respCode)) {
            this.respCode = "0000000";
        }

        return new BizTranInf(this);
    }

    public String getFlowId() {
        return this.flowId;
    }

    public String getBizId() {
        return this.bizId;
    }

    public String getTranCd() {
        return this.tranCd;
    }

    public String getIdNo() {
        return this.idNo;
    }

    public String getName() {
        return this.name;
    }

    public String getChannelNo() {
        return this.channelNo;
    }

    public String getSendTime() {
        return this.sendTime;
    }

    public String getReceiveTime() {
        return this.receiveTime;
    }

    public String getRequestData() {
        return this.requestData;
    }

    public String getResponseData() {
        return this.responseData;
    }

    public String getExeSts() {
        return this.exeSts;
    }

    public String getRespCode() {
        return this.respCode;
    }
}
